//
//  SSTimeViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/10/18.
//

import UIKit


public typealias SSTimeCallBack = (_ startTime:String,_ endTime:String)->()

class SSTimeViewController: UIViewController {
    
    @IBOutlet weak var closeBtn: UIButton!
    var callback:SSTimeCallBack = {startTime,endTime in }

   
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var startTimeBtn: UIButton!
    
    
    @IBOutlet weak var endTimeBtn: UIButton!
    
    var isStartTime = true
    var startTimeStr = ""
    var endTimeStr = ""
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        closeBtn.ssc_expandSize(size: 10)
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.1)
        
        startTimeBtn.layer.cornerRadius = 8
        startTimeBtn.layer.borderColor = UIColor(hex: "#2475FF")?.cgColor
        startTimeBtn.layer.borderWidth = 0.5
        
        endTimeBtn.layer.cornerRadius = 8
        endTimeBtn.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
        endTimeBtn.layer.borderWidth = 0.5
        
        datePicker.datePickerMode = .date
        datePicker.preferredDatePickerStyle = .wheels
        //将日期选择器区域设置为中文，则选择器日期显示为中文
        datePicker.locale = Locale(identifier: "zh_CN")
        //注意：action里面的方法名后面需要加个冒号“：”
        datePicker.addTarget(self, action: #selector(dateChanged),
                             for: .valueChanged)

        
    }
    
    func callback(_ finished: @escaping SSTimeCallBack) {
        callback = finished
    }
    
    //日期选择器响应方法
    @objc func dateChanged(datePicker : UIDatePicker){
        //更新提醒时间文本框
        let formatter = DateFormatter()
        //日期样式
//        formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
        formatter.dateFormat = "yyyy-MM-dd"
        print(formatter.string(from: datePicker.date))
        if(isStartTime){
            
            startTimeStr = formatter.string(from: datePicker.date)
            startTimeBtn.setTitle(startTimeStr, for: .normal)
        }else{
            
            endTimeStr = formatter.string(from: datePicker.date)
            endTimeBtn.setTitle(endTimeStr, for: .normal)
        }
        
        
    }
    
    @IBAction func startAction(_ sender: Any) {
        startTimeBtn.layer.borderColor = UIColor(hex: "#2475FF")?.cgColor
        endTimeBtn.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
        isStartTime = true
    }
    
    @IBAction func endAction(_ sender: Any) {
        endTimeBtn.layer.borderColor = UIColor(hex: "#2475FF")?.cgColor
        startTimeBtn.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
        isStartTime = false
    }
    
    @IBAction func doneAction(_ sender: Any) {
        if(startTimeStr.count == 0 || endTimeStr.count == 0){
            dissmissToTast(msg: "请选择时间")
            return
        }
        self.callback(startTimeStr,endTimeStr)
        self.dismiss(animated: true)
    }
    
    
    @IBAction func closeAction(_ sender: Any) {
        dismiss(animated: true)
    }
}
